package com.xmut.web;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.sys.entity.SysUser;
import com.sirdc.modules.sys.service.SysUserService;
import com.sirdc.modules.utils.StringUtils;
import com.xmut.databean.CommentListDatabean;
import com.xmut.databean.FrontDatabean;
import com.xmut.databean.TopicListDatabean;
import com.xmut.entity.Comment;
import com.xmut.filter.CommentFilter;
import com.xmut.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Controller
@RequestMapping("/comment")
public class FrontCommentController extends JsonBaseController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private SysUserService sysUserService;

    @Override
    protected String getView(String s) {
        return null;
    }

    /**
     * 评论列表
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public Message list(@ModelAttribute CommentFilter filter) {
        List<Comment> comments = commentService.query(filter);
        List<CommentListDatabean> databeans = new ArrayList<>();
        for (Comment comment : comments) {
            CommentListDatabean databean = null;
            SysUser user = sysUserService.getById(comment.getUserId());
            if (comment.getCommentId() != null) {
                Comment replyComment = commentService.getById(comment.getCommentId());
                SysUser user2 = sysUserService.getById(replyComment.getUserId());
                CommentListDatabean replyDatabean = new CommentListDatabean(user2, replyComment);
                databean = new CommentListDatabean(replyDatabean,user, comment);
            } else {
                databean = new CommentListDatabean(user, comment);
            }
            databeans.add(databean);

        }

        return coverMessage("200","",new FrontDatabean<CommentListDatabean>(filter.getPaging(),databeans));
    }

    /**
     *
     * 回复评论（评论的评论）
     * @param comment
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/pass/reply", method = RequestMethod.POST)
    public Message reply(@ModelAttribute Comment comment) {
        if(StringUtils.isBlank(comment.getComment())){
            return coverMessage("500","请输入评论内容！");
        }
        if(comment.getTopicId() == null || comment.getCommentId() == null){
            return coverMessage("500","参数错误");
        }
        commentService.saveComment(comment);

        return coverMessage("200");
    }




}
